<?php

namespace App\Kuafu\Base\Enum;

use App\Exceptions\BaseException;
use App\Library\FeiShu\SendFs;
use Illuminate\Support\Facades\Log;

class BaseCodeEnum
{
    //公共
    public const OK                  = 200;
    public const CREATED             = 201;
    public const NO_CONTENT          = 204;
    public const UNAUTHORIZED        = 401;
    public const FORBIDDEN           = 403;
    public const NOT_FOUND           = 404;
    public const NOT_ACCEPTABLE      = 406;
    public const UNPROCESABLE_ENTITY = 422;

    public const ERR_400000           = 400000;
    public const ERR_PARAM_PATH_ERROR = 400001;
    public const ERR_400002           = 400002;
    public const ERR_400003           = 400003;
    public const ERR_400009           = 400009;
    public const ERR_NO_ACCESS        = 400010;
    public const ERR_NO_ACCESS_ERROR  = 400011;
    public const ERR_NO_TOKEN         = 400012;
    public const ERR_ACCOUNT_BANNED   = 400013;
    public const ERR_EXPIRE_TOKEN     = 400014;
    public const ERR_TOKEN_ERROR      = 400015;  // 强制登出重新登录

    public const ERR_SERVER_ERROR   = 500000;
    public const ERR_DB_ERROR       = 500001;
    public const ERR_ROBOT_ERROR    = 500002;
    public const ERR_APPLET_TOKEN   = 500003;
    public const ERR_WM_CONN_TYPE   = 500004;
    public const ERR_ZIP_OPEN       = 500005;
    public const ERR_REMOTE_REQUEST = 500006;
    public const ERR_INVALID_FILE   = 500007;

    // 系统配置模块 - 输入异常405XXX
    public const ERR_SYSTEM_MENUS_PARAM_NAME      = 406001;
    public const ERR_SYSTEM_MENUS_PARAM_URL       = 406002;
    public const ERR_SYSTEM_MENUS_PARAM_PARENT_ID = 406003;
    public const ERR_SYSTEM_MENUS_PARAM_TYPE      = 406004;
    public const ERR_SYSTEM_MENUS_PARAM_STATUS    = 406005;
    public const ERR_SYSTEM_MENUS_PARAM_LOCATE    = 406006;
    public const ERR_SYSTEM_MENUS_PARAM           = 406007;
    public const ERR_SYSTEM_MENUS_PARAM_MENU_ID   = 406008;
    public const ERR_SYSTEM_MENUS_DELETE_ONE      = 406009;

    public const ERR_SYSTEM_MENUS_EDIT = 406010;
    public const ERR_SYSTEM_MENUS_DEL  = 406011;


    // 系统配置模块 - 系统异常505XXX


    public const CODE_TEXT = [
        // 公共信息内容
        self::OK                   => '成功',
        self::ERR_400000           => '参数校验错误',
        self::ERR_PARAM_PATH_ERROR => 'path参数值不合法',
        self::ERR_400002           => '登录状态失效,请重新登录',
        self::ERR_400003           => 'token过期,请重新刷新token',
        self::ERR_400009           => '非法数据',
        self::ERR_NO_ACCESS        => '无权访问',
        self::ERR_NO_ACCESS_ERROR  => '权限校验有误',
        self::ERR_ACCOUNT_BANNED   => '账号已经被禁用',
        self::ERR_TOKEN_ERROR      => '登录信息有误, 请重新登录', // 强制跳转至登录重新登录

        self::ERR_SERVER_ERROR                 => '服务器内部错误',
        self::ERR_DB_ERROR                     => '数据库处理出错',
        self::ERR_ZIP_OPEN                     => 'zip文件打开失败',
        self::ERR_REMOTE_REQUEST               => "远程请求失败",
        self::ERR_INVALID_FILE                 => '不合法的文件',

        // 系统配置模块
        self::ERR_SYSTEM_MENUS_PARAM_NAME      => "系统菜单名称有误",
        self::ERR_SYSTEM_MENUS_PARAM_URL       => "系统菜单URL名称有误",
        self::ERR_SYSTEM_MENUS_PARAM_PARENT_ID => "系统菜单上级菜单有误",
        self::ERR_SYSTEM_MENUS_PARAM_TYPE      => "系统菜单类型有误",
        self::ERR_SYSTEM_MENUS_PARAM_STATUS    => "系统菜单状态有误",
        self::ERR_SYSTEM_MENUS_PARAM_LOCATE    => "系统菜单位置有误",
        self::ERR_SYSTEM_MENUS_PARAM_MENU_ID   => "该系统菜单有误",
        self::ERR_SYSTEM_MENUS_DELETE_ONE      => "该系统菜单下还有其他菜单, 不能删除",
        self::ERR_SYSTEM_MENUS_EDIT            => "编辑系统菜单失败",
        self::ERR_SYSTEM_MENUS_DEL             => "删除系统菜单失败",
        self::ERR_NO_TOKEN                     => "token不存在",
        self::ERR_EXPIRE_TOKEN                 => "该二维码已失效",

        self::ERR_ROBOT_ERROR  => '不存在的飞书机器人',
        self::ERR_WM_CONN_TYPE => '错误的外卖connection类型',
        self::ERR_APPLET_TOKEN => 'APPLET-TOKEN解析错误',
    ];

    /**
     * 获取错误信息
     * @param $code
     * @return string
     */
    public static function getCodeText($code): string
    {
        // 先取子集
        $str = static::CODE_TEXT[$code] ?? '';
        if ($str) {
            return $str;
        }

        // 再取自己
        return self::CODE_TEXT[$code] ?? '';
    }

    /**
     * 自动通过模块转换错误信息
     * @param int $code
     * @return string
     */
    public static function getCodeTextAutoClass(int $code): string
    {
        $module = BaseException::getModule();

        // 生成xxxCodeEnum
        $enumClassName = "\App\Kuafu\\{$module}\Enum\\{$module}CodeEnum";
        $msg           = $enumClassName::getCodeText($code);
        if (!$msg) {
            $msg        = '网络错误，请稍后再试~';
            $warningMsg = "getCodeTextByClass Warning - code:{$code} resModule:{$module} enumClassName:{$enumClassName}";
            Log::warning($warningMsg);
            SendFs::newRobot()->setTitleContent('配置了错的EnumCode', $warningMsg)->send();
        }

        return $msg;
    }
}
